// pages/medicine/pages/doctorIntro/doctorIntro.js
var app = getApp()
var Time = require("../../../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuyue_status: false,
    proList: {
      spic: ["/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png"],
      name: '治疗胃癌',
      money: 20000,
      browse: ["/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png", "/assets/images/796.png"],
      accuracyTime: [{
          thatDay: '2018-12-11',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-12',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-13',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-14',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-15',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-16',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        },
        {
          thatDay: '2018-12-17',
          whatTime: [{
              spot: '09:00-10:00',
              people: 3
            },
            {
              spot: '11:00-12:00',
              people: 3
            },
            {
              spot: '14:00-15:00',
              people: 3
            },
            {
              spot: '16:00-17:00',
              people: 3
            },
            {
              spot: '17:00-18:00',
              people: 3
            }
          ]
        }
      ]
    },
    //时间段
    paragraph: [{
        spot: '09:00-10:00',
        people: 3
      },
      {
        spot: '11:00-12:00',
        people: 3
      },
      {
        spot: '14:00-15:00',
        people: 3
      },
      {
        spot: '16:00-17:00',
        people: 3
      },
      {
        spot: '17:00-18:00',
        people: 3
      }
    ],
    weekNum: 0, //选中那天
    spotNum: 0, //确定时间段
    //评论
    estimates: [{
        phoneMask: '小强',
        star: 1,
        img: '/assets/images/796.png',
        createTime: '2018-10-26',
        estimate: '用了都说好'
      },
      {
        phoneMask: '小强',
        star: 5,
        img: '/assets/images/796.png',
        createTime: '2018-10-26',
        estimate: '用了都说好'
      },
      {
        phoneMask: '小强',
        star: 3,
        img: '/assets/images/796.png',
        createTime: '2018-10-26',
        estimate: '用了都说好'
      },
      {
        phoneMask: '小强',
        star: 1,
        img: '/assets/images/796.png',
        createTime: '2018-10-26',
        estimate: '用了都说好'
      },
      {
        phoneMask: '小强',
        star: 5,
        img: '/assets/images/796.png',
        createTime: '2018-10-26',
        estimate: '用了都说好'
      }
    ],
    num: 1,
    readAclock: 0
  },
  browseThis: function(e) {
    console.log(e)
    let id = e.currentTarget.id;
    this.setData({
      num: id
    })
  },
  //打开预约时间弹窗
  agree: function() {
    let readAclock = this.data.readAclock;
    this.setData({
      readAclock: !this.data.readAclock
    })
  },
  //打开关闭时间弹窗
  noLook: function() {
    let readAclock = this.data.readAclock;
    this.setData({
      readAclock: !this.data.readAclock
    })
  },
  //选者那天
  subLevels: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let paragraph = that.data.paragraph;
    let accuracyTime = that.data.proList.accuracyTime;
    let arr = accuracyTime[index].whatTime;
    console.log(arr)
    that.setData({
      paragraph: arr,
      weekNum: index
    })
  },
  //选择几点
  paragraphIns: function(e) {
    console.log(e)
    let that = this;
    let index = e.currentTarget.dataset.index;
    let arrTiem = that.data.arrTiem;
    let paragraph = that.data.paragraph;
    this.setData({
      spotNum: index
    })
  },
  //确定多会
  determine: function() {
    let that = this;
    let readAclock = this.data.readAclock;
    let paragraph = that.data.paragraph;
    let weekNum = that.data.weekNum;
    let spotNum = that.data.spotNum;
    let accuracyTime = that.data.proList.accuracyTime;
    let tian = accuracyTime[weekNum].thatDay;
    let dian = paragraph[spotNum].spot;
    let OK = {
      tian: tian,
      dian: dian
    }
    console.log(OK);
    this.setData({
      readAclock: !this.data.readAclock
    })
    wx.navigateTo({
      url: '/hyb_yl/backstageGroceries/pages/information1/information1',
    })
  },
  //回到首页
  huiIndex: function() {
    wx.navigateTo({
      url: '/hyb_yl/tabBar/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    var sid = options.sid
    var zid = options.zid
    var userInfo = wx.getStorageSync('userInfo')
    var name = userInfo.nickName

    app.util.request({
      'url': 'entry/wxapp/Infofuwu',
      data: {
        sid: sid
      },
      success: function(res) {
        console.log(res)
        that.setData({
          suoltu: res.data.suoltu,
          servicebBox: res.data.servicebBox,
          info: res.data.info,
          stype: res.data.info.stype
        })
      }
    })
    that.setData({
      sid: sid,
      zid: zid,
      name: name
    })
    var openid = wx.getStorageSync('openid')
    app.util.request({
      'url': "entry/wxapp/Myinforsarray",
      data: {
        openid: openid,
      },
      success: function(res) {
        that.setData({
          mygerzl: res.data.data
        })
      },
      fail: function(err) {
        console.log(err)
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getFuwuinfos()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    let accuracyTime = that.data.proList.accuracyTime;
    let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    for (let i = 0; i < accuracyTime.length; i++) {
      let myDate = new Date(Date.parse(accuracyTime[i].thatDay));
      accuracyTime[i]['week'] = weekDay[myDate.getDay()];
    }
    that.setData({
      'proList.accuracyTime': accuracyTime
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  getFuwuinfos: function() {
    var that = this
    var sid = that.data.sid
    var zid = that.data.zid
    app.util.request({
      'url': 'entry/wxapp/Docfuwuinfo',
      data: {
        sid: sid,
        zid: zid,
        op: 'info'
      },
      success: function(res) {
        console.log(res)
        var arr = res.data
        var arrj = []
        var json2 = {}
        for (var texts in arr) {
          that.copy(res.data, json2)
          arrj = arrj.concat(json2[texts])
        }
        var arrs = Array.from(arrj)
        that.pin(arrs)
        var wArr = ["一", "二", "三", "四", "五", "六", "日"]
        for (var i = 0; i < 7; i++) {
          if (typeof arrs[i] == 'object') {
            arrs[i].week = "周" + wArr[i]
          } else {
            var obj = {}
            obj.week = "周" + wArr[i]
            arrs[i] = obj
          }
        }

        //今天周几
        var nDate = new Date()
        if (nDate.getDay() == 0) {
          var wg = 6
        } else {
          var wg = nDate.getDay() - 1
        }


        var tid = arrs[arrs.length - 1]
        arrs.splice(arrs.length - 1, 1)

        for (var x = 0; x < arrs.length; x++) {
          for (var y in arrs[x]) {
            if (y.indexOf("num") != -1) {
              if (arrs[x][y] == "") {
                arrs[x].dian = false
              } else {
                arrs[x].dian = true
              }
            }
          }
        }
        console.log(arrs)
        that.setData({
          selectdoctime: arrs,
          length: arrs.length,
          wg: wg,
          tid: tid,
          arr: arr,
        })
      }
    })
  },

  pin: function(arr1) {
    console.log(arr1)
    for (var i = 0, len = arr1.length; i < len; i++) {
      if (i < arr1.length - 1) {
        if (arr1[i].index !== "undefined" && arr1[i + 1].index !== "undefined") {
          if (arr1[i].index == arr1[i + 1].index) {
            for (var k in arr1[i]) {
              //console.log(k)
              if (k.indexOf("time") != -1) {
                arr1[i][k] += "-" + arr1[i + 1][k]
                // arr1[i][k] += "-" + arr1[i + 1][k]
              }
              if (k.indexOf("num") != -1) {
                arr1[i][k] += "-" + arr1[i + 1][k]
              }
            }
            arr1.splice(i + 1, 1)
            i--
          }
        }
      }
    }
  },
  copy: function(obj1, obj2) {
    var that = this
    var obj2 = obj2 || {}; //最初的时候给它一个初始值=它自己或者是一个json
    for (var name in obj1) {
      if (typeof obj1[name] === "object") { //先判断一下obj[name]是不是一个对象
        obj2[name] = (obj1[name].constructor === Array) ? [] : {}; //我们让要复制的对象的name项=数组或者是json
        that.copy(obj1[name], obj2[name]); //然后来无限调用函数自己 递归思想
      } else {
        obj2[name] = obj1[name]; //如果不是对象，直接等于即可，不会发生引用。
      }
    }
    return obj2; //然后在把复制好的对象给return出去
  },
  close_modal() {
    this.setData({
      yuyue_status: false
    })
  },
  yuyuw_xin(e) {
    var that = this
    var arrs = that.data.selectdoctime
    var indx = e.target.dataset.index
    var indxWeek = "week" + indx
    console.log(arrs, indx, indxWeek)
    for (var k in arrs[indx]) {
      if (k.indexOf("time") != -1) {
        // console.log(arrs[indx][k].indexOf("-"), k)
        if (arrs[indx][k].indexOf("-") != -1) {
          if (k.indexOf("endtime") != -1) {
            var endtime = arrs[indx][k].split("-")
          }
          if (k.indexOf("starttime") != -1) {
            var startime = arrs[indx][k].split("-")
            console.log(startime);
          }
        } else {
          if (k.indexOf("endtime") != -1) {
            var endtime = []
            endtime.push(arrs[indx][k])
          }
          if (k.indexOf("starttime") != -1) {
            var startime = []
            startime.push(arrs[indx][k])
            console.log(startime);
          }
        }
      }
      if (k.indexOf("num") != -1) {
        if (typeof arrs[indx][k] == "number") {
          arrs[indx][k] = String(arrs[indx][k])
        }
        if (arrs[indx][k].indexOf("-") != -1) {
          var nums = arrs[indx][k].split("-")
        } else {
          var nums = []
          nums.push(arrs[indx][k])
        }
      }
    }
    console.log(endtime, nums, startime)
    var star = []
    for (var i = 0, len = endtime.length; i < len; i++) {
      var obj = {}
      obj.sjstr = startime[i] + "-" + endtime[i]
      obj.shen = nums[i]
      obj.cont = nums[i]
      star[i] = obj
    }
    var selectdoctime = that.data.selectdoctime
    var week = selectdoctime[indx].index
    var aweek = selectdoctime[indx].week
    var todayDate = new Date();
    var date = todayDate.getDate();
    var month = todayDate.getMonth() + 1;
    var year = todayDate.getFullYear();
    var nweek = todayDate.getDay()
    if (nweek == 0) {
      nweek = 6
    } else {
      nweek--
    }
    var a = week - nweek
    date = date + a
    var curMonthDays = new Date(year, (month), 0).getDate();
    if (date > curMonthDays) {
      month++
      var b = date - curMonthDays
      date = 1 + b
    }
    if (month < 10) {
      month = "0" + month
    }
    if (date < 10) {
      date = "0" + date
    }
    var dangqianriqi = year + "-" + month + "-" + date
    console.log(star)
    that.setData({
      star: star,
      dangqianriqi: dangqianriqi,
      week: aweek,
      indxWeek: indxWeek,
      indx: indx
    })
    var tid = e.target.dataset.id
    app.util.request({
      'url': 'entry/wxapp/Select1',
      data: {
        tid: tid
      },
      success: function(res) {
        that.setData({
          yuyuebf: res.data.data
        })
      }
    })
    this.setData({
      yuyue_status: true
    })
  },
  choose_yy_time(e) {
    // 选择预约时间，去预约
    var that = this
    var arr = that.data.arr //星期数组
    var mygerzl = that.data.mygerzl
    var my_id = mygerzl.my_id //我的资料id
    var indexs = e.currentTarget.dataset.index;
    var arrs = that.data.star;
    var indx = that.data.indx
    var arrs = that.data.selectdoctime
    var indxWeek = "week" + indx

    for (var k in arrs[indx]) {
      if (k.indexOf("time") != -1) {
        // console.log(arrs[indx][k].indexOf("-"), k)
        if (arrs[indx][k].indexOf("-") != -1) {
          if (k.indexOf("endtime") != -1) {
            var endtime = arrs[indx][k].split("-")
          }
          if (k.indexOf("starttime") != -1) {
            var startime = arrs[indx][k].split("-")
          }
        } else {
          if (k.indexOf("endtime") != -1) {
            var endtime = []
            endtime.push(arrs[indx][k])
          }
          if (k.indexOf("starttime") != -1) {
            var startime = []
            startime.push(arrs[indx][k])
          }
        }
      }
      if (k.indexOf("num") != -1) {
        if (typeof arrs[indx][k] == "number") {
          arrs[indx][k] = String(arrs[indx][k])
        }
        if (arrs[indx][k].indexOf("-") != -1) {
          var nums = arrs[indx][k].split("-")
        } else {
          var nums = []
          nums.push(arrs[indx][k])
        }
      }
    }
    //console.log(endtime, nums)
    console.log(endtime);
    var star = []
    for (var i = 0, len = endtime.length; i < len; i++) {
      var obj = {}
      obj.sjstr = startime[i] + "-" + endtime[i]
      obj.shen = nums[i]
      obj.cont = nums[i]
      star[i] = obj
    }
    var tid = e.currentTarget.dataset.id
    var indxWeek = that.data.indxWeek;
    if (indxWeek == "week0") {
      indxWeek = indxWeek.slice(0, indxWeek.length - 1);
    }
    var week = that.data.week
    var name = that.data.name
    var zid = that.data.zid
    var z_yy_money = e.currentTarget.dataset.data;
    var openid = wx.getStorageSync('openid')
    if (star[indexs].shen > 0) {
      var zid = that.data.zid
      var openid = wx.getStorageSync('openid');
      var syperson = star[indexs].shen //剩余人数
      console.log(star[indexs])
      var times = that.data.dangqianriqi //选择年
      var yuyuetime = star[indexs].sjstr; //选择的时间段
      var arr0000 = star[indexs].sjstr.split("-")
      var endtime = arr0000[1]
      var startime = arr0000[0]
      var tttime = times + ' ' + yuyuetime //预约时间
      var week = that.data.week; //预约星期
      var stype = e.currentTarget.dataset.stype;
      console.log(stype)
      app.util.request({
        'url': "entry/wxapp/Myshifouyy",
        data: {
          zy_openid: openid,
          zy_type: tttime, //预约时间
          zy_riqi: week, //星期
        },
        success: function(res) {
          if (res.data.data !== false) {
            wx.showToast({
              title: '您已预约',
              icon: 'success',
              duration: 800,
            })
            return false;
          } else if (mygerzl == false) {
            wx.showModal({
              content: '请完善您的个人资料后再预约',
              success: function(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../gerenxinxi/gerenxinxi'
                  })
                }
              }
            })
            return false;
          } else {
            wx.showModal({
              content: '确认预约吗？',
              success: function(res) {
                var z_yy_money = e.currentTarget.dataset.data;
                var openid = wx.getStorageSync('openid')
                if (res.confirm) {
                  if (z_yy_money > 0) {

                    app.util.request({
                      'url': 'entry/wxapp/Orderpay',
                      method: "GET",
                      data: {
                        s_openid: openid,
                        s_ormoney: z_yy_money
                      },
                      success: function(res) {
                        wx.requestPayment({
                          'timeStamp': res.data.timeStamp,
                          'nonceStr': res.data.nonceStr, //字符串随机数
                          'package': res.data.package,
                          'signType': res.data.signType,
                          'paySign': res.data.paySign,
                          success: function(res) {
                            //更新预约人数
                            app.util.request({
                              'url': "entry/wxapp/Joninmoney",
                              data: {
                                use_openid: openid,
                                leixing: stype,
                                name: name,
                                pay: z_yy_money,
                                zid: zid,
                                types: 2
                              },
                              header: {
                                'content-type': 'application/json'
                              },
                              success: function(res) {
                                console.log(res)

                              },
                              fail: function(err) {
                                console.log(err)
                              },
                            })
                            var wekinfo = Array.from(arr[indxWeek])
                            for (var l in arr[indxWeek][indexs]) {
                              if (l.indexOf("num") != -1) {
                                console.log(arr[indxWeek][indexs]);
                                arr[indxWeek][indexs][l]--;
                              }
                            }
                            var xingqi = indxWeek
                            app.util.request({
                              'url': 'entry/wxapp/Upweekfuwu',
                              data: {
                                wekinfo: wekinfo,
                                xingqi: xingqi,
                                tid: tid,

                              },
                              success: function(res) {
                                console.log(res)
                                app.util.request({
                                  'url': "entry/wxapp/Userfuwuadd",
                                  data: {
                                    z_yy_money: z_yy_money,
                                    my_id: my_id,
                                    zid: zid,
                                    openid: openid,
                                    tttime: tttime,
                                    week: week,
                                    stype: stype,
                                    sid: that.data.sid,
                                  },
                                  success: function(res) {
                                    console.log(res)
                                    wx.showToast({
                                      title: '预约成功',
                                      success: function() {
                                        star[indexs].shen--;
                                        that.setData({
                                          star: star
                                        })
                                        setTimeout(function() {
                                          //要延时执行的代码
                                          // wx.navigateTo({
                                          //   url: '/hyb_yl/wodeyuyue/wodeyuyue'
                                          // })
                                        }, 2000) //延迟时间
                                      }
                                    })
                                    //发送模板消息给医生
                                    app.util.request({
                                      'url': 'entry/wxapp/Paywexdocmb',
                                      'data': {
                                        my_id: my_id, //用户ID
                                        zid: zid, //专家ID
                                        tttime: tttime
                                      },
                                      success: function(res) {
                                        console.log(res)
                                      },
                                      fail: function(err) {
                                        //console.log(err)
                                      },
                                    })
                                  },
                                  fail: function(err) {
                                    //console.log(err)
                                  },
                                })
                              }
                            })
                          },
                          fail: function(error) {
                            // console.log(error);
                          },
                        })
                      }
                    })
                  } else {
                    //更新预约人数
                    app.util.request({
                      'url': "entry/wxapp/Joninmoney",
                      data: {
                        use_openid: openid,
                        leixing: '挂号',
                        name: name,
                        pay: z_yy_money,
                        zid: zid
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success: function(res) {
                        console.log(res)

                      },
                      fail: function(err) {
                        console.log(err)
                      },
                    })

                    console.log(arr, indxWeek)
                    var wekinfo = Array.from(arr[indxWeek])
                    console.log(arr)
                    for (var l in arr[indxWeek][indexs]) {
                      if (l.indexOf("num") != -1) {
                        console.log(arr[indxWeek][indexs]);
                        arr[indxWeek][indexs][l]--;
                      }
                    }
                    var xingqi = indxWeek
                    var xingqi = indxWeek
                    app.util.request({
                      'url': 'entry/wxapp/Upweek',
                      data: {
                        wekinfo: wekinfo,
                        xingqi: xingqi,
                        tid: tid,

                      },
                      success: function(res) {
                        app.util.request({
                          'url': "entry/wxapp/Myzhuanjiayy",
                          data: {
                            zy_money: z_yy_money,
                            zy_name: my_id,
                            z_name: zid,
                            zy_openid: openid,
                            zy_type: tttime,
                            zy_riqi: week,
                            ksname: ksname,
                            syperson: syperson,
                            states: 1,
                            remove: 1,
                            paystate: 1
                          },
                          success: function(res) {
                            //console.log(res)
                            wx.showToast({
                              title: '预约成功',
                              success: function() {
                                star[indexs].shen--;
                                that.setData({
                                  star: star
                                })
                                app.util.request({
                                  'url': 'entry/wxapp/PaysendSmsgl',
                                  'data': {
                                    my_id: my_id, //用户ID
                                    zid: zid, //专家ID
                                    tttime: tttime
                                  },
                                  success: function(res) {
                                    //console.log(res)
                                  },
                                  fail: function(err) {
                                    // console.log(err)
                                  },
                                })
                                setTimeout(function() {
                                  //要延时执行的代码
                                  // wx.navigateTo({
                                  //   url: '/hyb_yl/wodeyuyue/wodeyuyue'
                                  // })
                                }, 2000) //延迟时间
                              }
                            })
                            //发送模板消息给医生
                            app.util.request({
                              'url': 'entry/wxapp/Paywexdocmb',
                              'data': {
                                my_id: my_id, //用户ID
                                zid: zid, //专家ID
                                tttime: tttime
                              },
                              success: function(res) {
                                console.log(res)
                              },
                              fail: function(err) {
                                //console.log(err)
                              },
                            })
                          },
                          fail: function(err) {
                            //console.log(err)
                          },
                        })
                      }
                    })
                  }

                }
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '已约满',
      })
    }
  },
})